package com.cn.unarmed.activity;

import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.cn.unarmed.adapter.CheckListFrameAdapter;
import com.cn.unarmed.base.BaseActivity;
import com.cn.unarmed.bean.DataHolder;
import com.cn.unarmed.commonutils.AppUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试可选中的listView
 */
public class CheckableFrameLayoutActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_list_frame);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    ListView listView;
    List<DataHolder> dataList;

    public void initView() {
        //构造数据
        dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            dataList.add(new DataHolder("harvic的blog------" + i, "harvic"));
        }
        //构造Adapter
        CheckListFrameAdapter adapter = new CheckListFrameAdapter(CheckableFrameLayoutActivity.this, dataList);
        listView = (ListView) findViewById(R.id.list_check);
        listView.setAdapter(adapter);
        AppUtils.setListViewHeightFrameLayout(listView);

        Button check_all = (Button) this.findViewById(R.id.check_all);
        check_all.setOnClickListener(this);
        Button check_none = (Button) this.findViewById(R.id.check_none);
        check_none.setOnClickListener(this);
        Button check_size = (Button) this.findViewById(R.id.check_size);
        check_size.setOnClickListener(this);
    }

    private static final String Tag = "CheckableFrameLayoutActivity";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.check_all:
                for (int i = 0; i < dataList.size(); i++) {
                    listView.setItemChecked(i, true);
                }
                break;
            case R.id.check_none:
                for (int i = 0; i < dataList.size(); i++) {
                    listView.setItemChecked(i, false);
                }
                break;
            case R.id.check_size:
                SparseBooleanArray check_list = listView.getCheckedItemPositions();
                if (check_list != null){
                    List<DataHolder> list_check = new ArrayList<>();
                    for (int i = 0; i < check_list.size(); i++) {
                        if (check_list.valueAt(i)) {
                            list_check.add(dataList.get(i));
                        }
                    }
                    Log.i("Tag","选中的条目有" + list_check.size() + "这么多条");
                }
                break;
        }
    }

    public void initData() {
    }
}
